<template>
  <view class="ucard-container">
    <z-paging ref="paging" v-model="cardList" @query="queryList">
      <template #top>
        <!-- 顶部导航栏 -->
        <u-navbar :border="false" bg-color="#fff" title-color="#333" :safe-area-inset-top="true" placeholder>
          <template #center>
            <!-- 顶部标签切换 -->
            <view class="tabs-container">
              <view class="tab-item" :class="{ active: activeTab === 'all' }" @click="switchTab('all')">
                <text class="tab-text">{{ useT('全部卡') }}</text>
              </view>
              <view class="tab-item" :class="{ active: activeTab === 'my' }" @click="switchTab('my')">
                <text class="tab-text">{{ useT('我的卡') }}</text>
              </view>
            </view>
          </template>
        </u-navbar>
        <!-- 筛选器 -->
        <view class="filter-container">
          <view class="filter-item" @click="showFilter('type')">
            <text class="filter-label">{{ useT('卡片类型') }}:</text>
            <text class="filter-value">{{ selectedType }}</text>
            <u-icon name="arrow-down" size="12" color="#999"></u-icon>
          </view>
          <view class="filter-item" @click="showFilter('affiliation')">
            <text class="filter-label">{{ useT('所属类型') }}:</text>
            <text class="filter-value">{{ selectedAffiliation }}</text>
            <u-icon name="arrow-down" size="12" color="#999"></u-icon>
          </view>
          <view class="filter-item" @click="showFilter('status')">
            <text class="filter-label">{{ useT('卡片状态') }}:</text>
            <text class="filter-value">{{ selectedStatus }}</text>
            <u-icon name="arrow-down" size="12" color="#999"></u-icon>
          </view>
        </view>
      </template>

      <!-- 页面内容 -->
      <view class="content">
        <!-- 卡片列表 -->
        <view class="card-list">
          <view class="card-item" v-for="(card, index) in cardList" :key="card.id || index" @click="viewCard(card)">
            <view class="card-left">
              <image :src="card.image" class="card-image" mode="aspectFill"></image>
            </view>
            <view class="card-right">
              <view class="card-info">
                <text class="card-title">{{ useT(card.title) }}</text>
                <text class="card-desc">{{ useT(card.maxtitle) }}</text>
                <text class="card-applicants">{{ card.virtual_num }} {{ useT('人已申请') }}</text>
                <view class="card-price-wrapper">
                  <text class="card-price">{{ card.price }} {{ useT('USDT') }}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </z-paging>

    <!-- 自定义 Tabbar -->
    <CustomTabbar :currentIndex="1" />
  </view>
</template>

<script setup>
import { getUcardList } from '@/api/project';
import CustomTabbar from '@/components/CustomTabbar.vue';
import { useT } from '@/utils/i18n';
import { onShow } from '@dcloudio/uni-app';
import { onMounted, ref } from 'vue';

// 响应式数据
const activeTab = ref('all');
const selectedType = ref('all');
const selectedAffiliation = ref('all');
const selectedStatus = ref('all');
const cardList = ref([]);
const paging = ref(null);
onShow(() => {
  console.log('onShow');
  uni.hideTabBar();
});

// z-paging 查询方法
const queryList = async (pageNum, pageSize) => {
  console.log(`加载第${pageNum}页，每页${pageSize}条数据`);

  try {
    const res = await getUcardList({
      page: pageNum,
      limit: pageSize
    });

    // 处理接口返回的数据
    if (res.status === 200 && res.data && Array.isArray(res.data)) {
      // 直接使用接口返回的数据
      paging.value.complete(res.data);
    } else {
      // 数据格式异常
      console.error('接口返回数据格式异常:', res);
      paging.value.complete([]);
    }
  } catch (error) {
    console.error('获取U卡列表失败:', error);
    // 请求失败，显示空数据
    paging.value.complete([]);
    // 可选：显示错误提示
    uni.showToast({
      title: useT('加载失败，请重试'),
      icon: 'none',
      duration: 2000
    });
  }
};

// 方法
const switchTab = (tab) => {
  activeTab.value = tab;
  // 切换标签时重新加载数据
  paging.value.reload();
};

const showFilter = (type) => {
  console.log('显示筛选器:', type);
  // 这里可以显示筛选器弹窗
};

const viewCard = (card) => {
  console.log('查看卡片:', card);
  // 跳转到卡片申请页面
  uni.navigateTo({
    url: `/pagesA/cardApply/cardApply?id=${card.id}`
  });
};

// 生命周期
onMounted(() => {
  console.log('U卡页面加载完成');
});
</script>

<style lang="scss" scoped>
.ucard-container {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding-bottom: 120rpx; /* 为 tabbar 留出空间 */
}

// 导航栏中的标签切换样式
.tabs-container {
  display: flex;
  background-color: #f5f5f5;
  border-radius: 16rpx;
  padding: 2rpx;
  width: 280rpx;

  .tab-item {
    flex: 1;
    text-align: center;
    padding: 8rpx 0;
    border-radius: 14rpx;
    transition: all 0.3s ease;

    &.active {
      background-color: #ffffff;
      box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
    }

    .tab-text {
      font-size: 24rpx;
      font-weight: 500;
      color: #666;

      .active & {
        color: #333;
        font-weight: 600;
      }
    }
  }
}
// 筛选器
.filter-container {
  display: flex;
  margin: 20rpx;

  .filter-item {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16rpx 12rpx;
    background-color: #ffffff;
    border-radius: 12rpx;
    margin-right: 12rpx;

    &:last-child {
      margin-right: 0;
    }

    .filter-label {
      font-size: 24rpx;
      color: #666;
      margin-right: 8rpx;
    }

    .filter-value {
      font-size: 24rpx;
      color: #333;
      margin-right: 8rpx;
    }
  }
}
.content {
  padding: 0rpx 24rpx;
  // 卡片列表
  .card-list {
    .card-item {
      display: flex;
      background-color: #ffffff;
      border-radius: 16rpx;
      padding: 24rpx;
      margin-bottom: 16rpx;
      box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
      transition: all 0.3s ease;

      &:active {
        transform: scale(0.98);
      }

      .card-left {
        margin-right: 24rpx;

        .card-image {
          width: 210rpx;
          height: 130rpx;
          border-radius: 8rpx;
        }
      }

      .card-right {
        flex: 1;
        display: flex;
        align-items: flex-start;

        .card-info {
          flex: 1;
          display: flex;
          flex-direction: column;

          .card-title {
            display: block;
            font-size: 32rpx;
            font-weight: 600;
            color: #333;
            margin-bottom: 8rpx;
            line-height: 1.3;
          }

          .card-desc {
            display: block;
            font-size: 24rpx;
            color: #666;
            margin-bottom: 8rpx;
            line-height: 1.4;
          }

          .card-applicants {
            display: block;
            font-size: 20rpx;
            color: #999;
            line-height: 1.2;
            margin-bottom: 8rpx;
          }

          .card-price-wrapper {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            margin-top: 4rpx;

            .card-price {
              font-size: 28rpx;
              color: #12c284;
              font-weight: 600;
              line-height: 1.2;
            }
          }
        }
      }
    }
  }
}
</style>
